using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Meta.Editor
{
	/// <summary>
	/// Summary description for FindDialog.
	/// </summary>
	public class ReplaceDialog : System.Windows.Forms.Form
	{
		public System.Windows.Forms.TextBox searchText;
		private System.Windows.Forms.Button findButton;
		public System.Windows.Forms.TextBox replacementText;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button replaceButton;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Button doneButton;
		CodeBox code;
		bool done=false;

		public ReplaceDialog(CodeBox code)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			this.code = code;
			string selection = code.SelectedText;
			if (selection.Length==0)
				searchText.Text = FindDialog.lastSearch;
			else
				searchText.Text = selection;

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.searchText = new System.Windows.Forms.TextBox();
			this.findButton = new System.Windows.Forms.Button();
			this.doneButton = new System.Windows.Forms.Button();
			this.replacementText = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.replaceButton = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// searchText
			// 
			this.searchText.Location = new System.Drawing.Point(8, 24);
			this.searchText.Name = "searchText";
			this.searchText.Size = new System.Drawing.Size(208, 20);
			this.searchText.TabIndex = 0;
			this.searchText.Text = "";
			// 
			// findButton
			// 
			this.findButton.Location = new System.Drawing.Point(232, 8);
			this.findButton.Name = "findButton";
			this.findButton.TabIndex = 2;
			this.findButton.Text = "&Find next";
			this.findButton.Click += new System.EventHandler(this.FindButton_Click);
			// 
			// doneButton
			// 
			this.doneButton.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.doneButton.Location = new System.Drawing.Point(232, 72);
			this.doneButton.Name = "doneButton";
			this.doneButton.TabIndex = 4;
			this.doneButton.Text = "&Done";
			this.doneButton.Click += new System.EventHandler(this.doneButton_Click);
			// 
			// replacementText
			// 
			this.replacementText.Location = new System.Drawing.Point(8, 72);
			this.replacementText.Name = "replacementText";
			this.replacementText.Size = new System.Drawing.Size(208, 20);
			this.replacementText.TabIndex = 1;
			this.replacementText.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.TabIndex = 4;
			this.label1.Text = "Find:";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 56);
			this.label2.Name = "label2";
			this.label2.TabIndex = 5;
			this.label2.Text = "And replace with:";
			// 
			// replaceButton
			// 
			this.replaceButton.Location = new System.Drawing.Point(232, 40);
			this.replaceButton.Name = "replaceButton";
			this.replaceButton.TabIndex = 3;
			this.replaceButton.Text = "&Replace";
			this.replaceButton.Click += new System.EventHandler(this.ReplaceButton_Click);
			// 
			// ReplaceDialog
			// 
			this.AcceptButton = this.findButton;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.doneButton;
			this.ClientSize = new System.Drawing.Size(320, 110);
			this.Controls.Add(this.replaceButton);
			this.Controls.Add(this.replacementText);
			this.Controls.Add(this.searchText);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.doneButton);
			this.Controls.Add(this.findButton);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "ReplaceDialog";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Replace text";
			this.ResumeLayout(false);

		}
		#endregion

		private void FindButton_Click(object sender, System.EventArgs e) {
			FindDialog.lastSearch = searchText.Text;
			if (code.Find(searchText.Text, code.SelectionStart+code.SelectionLength, RichTextBoxFinds.None)<0)
				Done();
			code.HideSelection = false;
		}

		private void ReplaceButton_Click(object sender, System.EventArgs e) {
			if (!done) {
				code.SelectedText = replacementText.Text;
				if (code.Find(searchText.Text, code.SelectionStart+code.SelectionLength, RichTextBoxFinds.None)<0)
					Done();
				else
					replaceButton.Select();
				code.HideSelection = false;
			}
		}

		private void doneButton_Click(object sender, System.EventArgs e) {
			code.HideSelection = true;
			this.Close();
			CodeBox.SuppressKeyUp(Keys.Enter);
		}

		void Done() {
			doneButton.Select();
			done = true;
		}
	}
}
